package com.cuitrip.util;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cuitrip.service.R;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;

/**
 * Created by MiJack on 2015/11/9.
 */
public class GoogleMapFragment extends CustomUiFragment implements OnMapReadyCallback {

    private double lat;
    private double lng;
    private String name;
    private View root;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        lat = arguments.getDouble(MapController.VALUE_LAT, 0);
        lng = arguments.getDouble(MapController.VALUE_LNG, 0);
        name = arguments.getString(MapController.VALUE_NAME);
        root= super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_google_map);
        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        return root;
    }

    @Override
    public void onMapReady(GoogleMap map) {
        map.addMarker(new MarkerOptions()
                .position(new LatLng(lat, lng))
                .title(name));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 18));
        UiSettings settings = map.getUiSettings();
        settings.setMapToolbarEnabled(true);
        settings.setAllGesturesEnabled(true);
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarMiddleAreaName=getString(R.string.trip_attribute_meet,name);
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        getActivity().finish();
    }
}
